<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("hotelWrite.htm");
include "setting.php";
include "../include/function.php";

$old_hotel_image=$_POST['old_hotel_image'];

$country_id=intval($_POST['country_id']);
$city_id=intval($_POST['city_id']);
$hotel_name=formatDataForMySql($_POST['hotel_name']);
$hotel_brief=formatDataForMySql($_POST['hotel_brief']);
$hotel_overview=formatData($_POST['hotel_overview']);
$hotel_indepth=formatData($_POST['hotel_indepth']);
$hotel_location=formatData($_POST['hotel_location']);
$hotel_traveladvice=formatData($_POST['hotel_traveladvice']);
$hotel_rates=formatDataForMySql($_POST['hotel_rates']);
$hotel_position=intval($_POST['hotel_position']);
$hotel_ishome=intval($_POST['hotel_ishome']);
$hotel_isdestination=intval($_POST['hotel_isdestination']);
$hotel_isactive=intval($_POST['hotel_isactive']);
$hotel_metadescription=formatDataForMySql($_POST['hotel_metadescription']);
$hotel_metakeyword=formatDataForMySql($_POST['hotel_metakeyword']);

$ID=intval(_POST('ID'));
$type=$_POST['type'];

//
$date = date("Y-m-d");
$sub_mit = $_POST['sub_mit'];
if($sub_mit=="yes"){
	if($hotel_name==""){
		$error_hotel_name ="Bạn phải nhập Tên <br />";
	}
	//UPLOAD NEW FILE
	if(!$DELETE_ORIGIN_IMAGE)
	{
		if($err=="")
		{
			if($_FILES['hotel_image'])
			{
				$hotel_image=uploadfile($path, $_FILES['hotel_image'],0,$MAX_FILE_SIZE);
			}
		}
		if($hotel_image=='-1')
		{
			$error_hotel_image.="Lỗi, không thể upload";
			$err="yes";
		}
		else if($hotel_image=='0')
		{
			$error_hotel_image.="File không đúng kiểu";
			$err="yes";
		}
		else if($hotel_image=='1')
		{
			$max_file_size=$MAX_FILE_SIZE/1024;
			$error_hotel_image.="Ảnh quá lớn";
			$err="yes";
		}
		else if($hotel_image=='2')
		{
			$hotel_image="";
		}	
		
		$hotel_image_tmp=$hotel_image;
	}
	else
	{
		if($_FILES['hotel_image']['name'])
		{
			$myfile=str_replace(" ","_",$_FILES['hotel_image']['name']);
			$hotel_image_tmp=time()."_".$myfile;
			$no_origin=1;
		}
	}
	if($err=="" && $hotel_image_tmp!="")
	{
		//Create thumbnail
		if($IS_CREATE_THUMB){
			for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
			{
				$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
				thumbnail_images($_FILES['hotel_image'], $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $hotel_image_tmp, 0, $text); 
			}
		}
		//if($DELETE_ORIGIN_IMAGE)
//		{
//			deletefile($path.$hotel_image_tmp);
//		}
		if(count($MAX_THUMB_WIDTH) && $no_origin==1)
		{
			$hotel_image=str_replace(" ","_",$hotel_image_tmp);
		}
	}
	
	$str_error=$error_hotel_name;
	if($str_error==""){
		if($type=="ADD"){	
			$sql="INSERT INTO ".pre_table."hotel(country_id, city_id, hotel_name, hotel_brief, hotel_image, hotel_overview, hotel_indepth, hotel_location, hotel_traveladvice, hotel_rates, hotel_position, hotel_ishome, hotel_isdestination, hotel_isactive, hotel_metadescription, hotel_metakeyword, lang) 
					VALUES	('".$country_id."', '".$city_id."', '".$hotel_name."', '".$hotel_brief."', '".$hotel_image."', '".$hotel_overview."', '".$hotel_indepth."', '".$hotel_location."', '".$hotel_traveladvice."', '".$hotel_rates."', '".$hotel_position."', '".$hotel_ishome."', '".$hotel_isdestination."', '".$hotel_isactive."', '".$hotel_metadescription."', '".$hotel_metakeyword."', '".$lang."')";
			execSQL($sql);
		}else{
			//Create thumbnail
			if($IS_CREATE_THUMB && $old_hotel_image){
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
					thumbnail_images('', $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $old_hotel_image, 0, $text); 
				}
			}
			
		    if($old_hotel_image && $hotel_image)
			{
				deletefile($path.$old_hotel_image);
				//Delete thumbnail
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					deletefile($path."thumb_".$MAX_THUMB_WIDTH[$i]."_".$old_hotel_image);
				}
			}
			if(!$hotel_image && $old_hotel_image)
			{
				$hotel_image=$old_hotel_image;
			}
			
			$sql="UPDATE ".pre_table."hotel SET 
						country_id='".$country_id."', 
						city_id='".$city_id."', 
						hotel_name='".$hotel_name."', 
						hotel_brief='".$hotel_brief."',
						hotel_image='".$hotel_image."', 
						hotel_overview='".$hotel_overview."', 
						hotel_indepth='".$hotel_indepth."', 
						hotel_location='".$hotel_location."', 
						hotel_traveladvice='".$hotel_traveladvice."', 
						hotel_rates='".$hotel_rates."', 
						hotel_position='".$hotel_position."', 
						hotel_ishome='".$hotel_ishome."',
						hotel_isdestination='".$hotel_isdestination."',
						hotel_isactive='".$hotel_isactive."',
						hotel_metadescription='".$hotel_metadescription."', 
						hotel_metakeyword='".$hotel_metakeyword."', 
						lang='".$lang."'
						WHERE hotel_id='".$ID."'";			
			execSQL($sql);
		}			

		$act = $_POST['act'];
		if($act=='save'){
			echo '<script>
				parent.document.frmData.target = "_parent";				
				parent.window.location.href="hotel.php";
			</script>';
		}else{	
			echo '<script>
				parent.document.frmData.target = "_parent";	
				parent.document.frmData.hotel_name.focus();			
				parent.window.location.href="hotelWrite.php";
			</script>';
		}	
		
	}else{
		echo '<script>
				parent.document.frmData.target = "_parent";		
				parent.document.getElementById("err").style.display="block";
				parent.document.getElementById("err_string").innerHTML="'.$str_error.'";
			</script>';	
	}
}
?>